<template>
  <div class="goodsBox">
    <!-- 面包屑 -->
    <MBX level1="商品管理" level2="商品列表"></MBX>

    <!-- 卡片 -->
    <el-card class="mt16">
      <!-- 搜索框
       
       -->
        <el-col :span="12">
          <el-input
            clearable
            placeholder="请输入搜索内容"
            class="input-with-select"
            v-model="goodsInfo.query"
            @input="searchUser"
           
          >
            <el-button
              
              slot="append"
              icon="el-icon-search"
               @click="searchUser"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 添加按钮 @click="userBtn" -->
        <el-col :span="6">
          <el-button class="goodsBtn" type="primary" @click="goodsBtn" >添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table border   :data="goodsData" style="width: 100%">
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="300px"></el-table-column>
        <el-table-column label="商品价格（元）" prop="goods_number"></el-table-column>
        <el-table-column label="商品重量" prop="goods_price"></el-table-column>
        <el-table-column label="创建时间" prop="add_time">
          <template slot-scope="scope">
            <div>
              {{ scope.row.add_time | dataTime }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <div>
              <!-- 编辑   -->
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
               @click="edit(scope.row)" 
              ></el-button>
              <!-- 删除   -->
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="del(scope.row)"
              ></el-button>
              
            </div>
          </template>
        </el-table-column>
      </el-table>

      

       <!-- 分页 -->
      <div class="block mt16">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="goodsInfo.pagenum"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="goodsInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

 

    
  </div>
</template>

<script>
import {goodsApi,goodsDelApi} from '../../../http/api'
import _ from "lodash";
export default {
  data() {
    return {
      total: 0, // 总页数
      // 数据
      goodsInfo:{
        query:'',
        pagenum:1,
        pagesize:5,
      },
      goodsData:[], // 列表数据
      goodsState:false, // 添加的弹出框状态
      editBox:false,//编辑的弹出框状态
      
    }
  },
  methods: {
     //   点击编辑按钮，跳转页面
    edit(row) {
      // console.log("编辑的这一行数据：", row);
      this.$router.push({path:'/goodEdit',query:{row}})
    },

    // 点击弹出添加的模态框
    goodsBtn(){
      // console.log('点击了添加');
      this.$router.push('/goodsAdd')
    },
    // 搜索
     searchUser: _.debounce(function () {
      this.goodsList()
    }, 1000), // 做一个3秒
    // 删除
    del(val) {
      // console.log("删除", val.goods_id);
      this.$confirm("是否要删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })

        .then(async () => {
          const res = await this.$API.goodsDelApi(Number(val.goods_id));
          this.goodsList()

          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取列表渲染数据
    async  goodsList(){
      const res = await goodsApi(this.goodsInfo)
      // console.log('列表渲染数据:',res.goods);
      this.total = res.total
      this.goodsData = res.goods
    },
    //   分页
    handleSizeChange(val) {
      this.goodsInfo.pagesize = val;
       this.goodsList()
    },
    handleCurrentChange(val) {
      this.goodsInfo.pagenum = val;
       this.goodsList()
    },

  },
  created() {
    this.goodsList()
  },
}
</script>

<style lang="scss" scoped>
// 添加按钮
.goodsBtn{
  margin-left: 15px;
  margin-bottom: 16px;
}

</style>

